package com.example.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
public class IpCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {

        String localIp=getLocalIp();
        String runIp=conditionContext.getEnvironment().getProperty("schedule.ip");

        boolean equals=runIp.equals(localIp);
        log.info("localIp={},runIp={},equals={}",localIp,runIp,equals);
        return equals;
    }

    private String getLocalIp() {
        InetAddress address=null;
        try {
            address=InetAddress.getLocalHost();
            return address.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return "null";
    }
}
